/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
    View,
    Navigator,
    BackAndroid
} from 'react-native';

let globalNav = {};
export default class CommonNavigator extends Component {
  onBackAndroid() {
    let routes = globalNav.getCurrentRoutes();
    if(routes.length > 1) {
      globalNav.pop();
      return true;
    }else {
      return false;
    }
  }
  render() {
    let defaultName = this.props.initName;
    let defaultComponent = this.props.initComponent;
    return (
      <Navigator
        initialRoute={{ name: defaultName, component: defaultComponent }}
        configureScene={(route) => {
          return Navigator.SceneConfigs.FadeAndroid;
        }}
        renderScene={(route, navigator) => {
          globalNav = navigator;
          BackAndroid.addEventListener('hardwareBackPress', this.onBackAndroid.bind(this));
          let Component = route.component;
          return <Component {...route.params} navigator={navigator} />
        }} />
      )
  }
}
